#!/usr/bin/env bash

function modify_dns_config() {
    script_dir=$1
    src="${script_dir}/setClusterCommon/files/resolv.conf"
    dest="/etc/resolv.conf"
    copy_file ${src} ${dest}
}

function back_up_apt_sourcefile(){
    case "$ID" in
      ubuntu*)
        cp /etc/apt/sources.list /etc/apt/sources.list.old
        ;;
      rhel*|centos*)
        yum install -y wget
        mkdir -p /etc/yum.repos.d/bak && mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak
        ;;
    esac
}

function replace_apt_sourcefile(){
    case "$ID" in
      ubuntu*)
        script_dir=$1
        src="${script_dir}/setClusterCommon/files/sources.list"
        dest="/etc/apt/sources.list"
        copy_file ${src} ${dest}
        ;;
      rhel*|centos*)
        wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.tencent.com/repo/centos7_base.repo
        wget -O /etc/yum.repos.d/epel.repo http://mirrors.cloud.tencent.com/repo/epel-7.repo
        yum clean all && yum makecache
        ;;
    esac

}

function install_dependeces(){
    install_software curl
}

function configure_k8s_apt(){
    case "$ID" in
      ubuntu*)
        curl -s https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add -

        script_dir=$1
        src="${script_dir}/setClusterCommon/files/kubernetes.list"
        dest="/etc/apt/sources.list.d/kubernetes.list"
        copy_file ${src} ${dest}

        apt-get update
        apt --fix-broken install
        ;;
      rhel*|centos*)
        script_dir=$1
        src="${script_dir}/setClusterCommon/files/kubernetes.repo"
        dest="/etc/yum.repos.d/kubernetes.repo"
        copy_file ${src} ${dest}
        ;;
    esac

}

function configure_firewall(){
    install_software ufw
    ufw disable
}

function configure_SELINUX(){
    #setenforce 0

    script_dir=$1
    src="${script_dir}/setClusterCommon/files/selinuxconfig"
    dest="/etc/selinux/config"
    copy_file ${src} ${dest}
}

function configure_ip_forward(){
    replace_lastmatch  '^iptables -P FORWARD ACCEPT$' 'iptables -P FORWARD ACCEPT' /etc/rc.local
    if [[ $? -ne 0 ]];then
        insert_after_lastmatch '^#' 'iptables -P FORWARD ACCEPT' /etc/rc.local
    fi
}

function configure_neworking(){

    configure_ip_forward

    script_dir=$1
    src="${script_dir}/setClusterCommon/files/sysctl.conf"
    dest="/etc/sysctl.conf"
    copy_file ${src} ${dest}

    script_dir=$1
    src="${script_dir}/setClusterCommon/files/k8snet.conf"
    dest="/etc/sysctl.d/k8s.conf"
    copy_file ${src} ${dest}

    modprobe br_netfilter

    echo "br_netfilter" > /etc/modules-load.d/br_netfilter.conf

    sysctl --system

    iptables -P FORWARD ACCEPT
}

function close_swap(){
    swapoff -a
    sed -i 's/.*swap.*/#&/' /etc/fstab
}

function configure_ipvs_proxy(){
    install_software ipset

    mkdir -p /etc/sysconfig/modules
    script_dir=$1
    src="${script_dir}/setClusterCommon/files/ipvs.modules"
    dest="/etc/sysconfig/modules/ipvs.modules"
    copy_file ${src} ${dest}
    chmod 755 /etc/sysconfig/modules/ipvs.modules

    bash /etc/sysconfig/modules/ipvs.modules
    lsmod | grep -e ip_vs -e nf_conntrack_ipv4
}

function configure_timeinfo(){
    timedatectl set-timezone Asia/Shanghai
    timedatectl
}

function replace_or_add() {
    match_ip=${1}
    dest_str=${2}
    file=${3}

    sed -i "/${match_ip}/d" ${file}
    echo "${dest_str}" >>${file}

}

function merege_hosts() {
    file_path=$1

    while read line; do
        echo adding "${line}" to /etc/hosts
        ip=$(echo ${line} | awk '{print $1}' )
        replace_or_add "${ip}" "${line}" "/etc/hosts"
    done < "${file_path}"
}


# step1 setClusterCommon
function set_cluster_common(){
    modify_dns_config ${SCRIPT_DIR}
    back_up_apt_sourcefile
    replace_apt_sourcefile ${SCRIPT_DIR}
    install_dependeces
    configure_k8s_apt ${SCRIPT_DIR}
    configure_firewall
    configure_SELINUX ${SCRIPT_DIR}
    configure_ip_forward
    configure_neworking ${SCRIPT_DIR}
    close_swap
    # configure_ipvs_proxy ${SCRIPT_DIR}
    configure_timeinfo

    merege_hosts "${SCRIPT_DIR}/setClusterCommon/files/hosts"
}